﻿using ModuleBase;
using NModbus;
using NModbus.Serial;
using Prism.Ioc;
using System.Diagnostics;
using System.IO.Ports;

namespace ModbusCon
{
    public class ModbusCon : ModuleBase.ModuleBase
    {
        bool outputs = false;
        bool isExecuted = false;
        private IContainerProvider _containerProvider;
        private IServoModbusService servoModbus;

        private string _parameter1 = "COM1";
        public string Parameter1
        {
            get => _parameter1;
            set
            {
                if (_parameter1 != value)
                {
                    _parameter1 = value;
                    OnPropertyChanged(nameof(Parameter1));
                }
            }
        }
        private int _parameter2 = 9600;
        public int Parameter2
        {
            get => _parameter2;
            set
            {
                if (_parameter2 != value)
                {
                    _parameter2 = value;
                    OnPropertyChanged(nameof(Parameter2));
                }
            }
        }
        private string _parameter3 = "无校验，8bit数据位，1个停止位";
        public string Parameter3
        {
            get => _parameter3;
            set
            {
                if (_parameter3 != value)
                {
                    _parameter3 = value;
                    OnPropertyChanged(nameof(Parameter3));
                }
            }
        }
        private int _parameter4 = 500;
        public int Parameter4
        {
            get => _parameter4;
            set
            {
                if (_parameter4 != value)
                {
                    _parameter4 = value;
                    OnPropertyChanged(nameof(Parameter4));
                }
            }
        }
        private int _parameter5 = 500;
        public int Parameter5
        {
            get => _parameter5;
            set
            {
                if (_parameter5 != value)
                {
                    _parameter5 = value;
                    OnPropertyChanged(nameof(Parameter5));
                }
            }
        }
        private string _parameter6 = "开";
        public string Parameter6
        {
            get => _parameter6;
            set
            {
                if (_parameter6 != value)
                {
                    _parameter6 = value;
                    OnPropertyChanged(nameof(Parameter6));
                }
            }
        }
        public override Dictionary<string, object> GetProperties()
        {
            return new Dictionary<string, object>
            {
                { "端口", Parameter1 },
                { "波特率", Parameter2 },
                { "Modbus数据格式", Parameter3 },
                { "读取超时", Parameter4 },
                { "写入超时", Parameter5 },
                { "开关", Parameter6 },
            };
        }

        public override Dictionary<string, List<object>> PropertyOptions { get; } = new Dictionary<string, List<object>>
        {
            { "端口", new List<object> {  "COM1", "COM2", "COM3", "COM4", "COM5", "COM6", "COM7", "COM8", "COM9", "COM10", "COM11", "COM12" } },
            { "波特率" ,new List<object>{ 2400, 4800, 9600 , 19200 , 38400 , 57600 ,115200} },
            { "Modbus数据格式" ,new List<object>{ "无校验，8bit数据位，2个停止位", "偶校验，8bit数据位，1个停止位", "奇校验，8bit数据位，1个停止位", "无校验，8bit数据位，1个停止位" } },
            { "开关" ,new List<object>{ "开","关" } },
        };


        public ModbusCon(IContainerProvider containerProvider)
        {
            _containerProvider = containerProvider;
            Name = "Modbus通讯连接";

            DisplayNameToPropertyNameMap = new Dictionary<string, string>
            {
                { "端口", nameof(Parameter1) },
                { "波特率", nameof(Parameter2) },
                { "Modbus数据格式", nameof(Parameter3) },
                { "读取超时", nameof(Parameter4) },
                { "写入超时", nameof(Parameter5) },
                { "开关", nameof(Parameter6) }
            };
        }

        public override bool Execute()
        {
            Status = ModuleStatus.Executing; // 设置状态为执行中

            if (isExecuted)
            {
                outputs = true;
                return outputs;
            }
            var containerExtension = _containerProvider.Resolve<IContainerExtension>();
            if (!_containerProvider.IsRegistered<IServoModbusService>(Parameter1))
            {
                containerExtension.RegisterSingleton<IServoModbusService, ServoModbusService>(Parameter1);
            }

            servoModbus = _containerProvider.Resolve<IServoModbusService>(Parameter1);

            if (Parameter6 == "开")
            {
                if (servoModbus.Connect(Parameter1,Parameter2, Parameter3, Parameter4, Parameter5))
                {
                    outputs = true;
                    isExecuted = true;
                }
                else
                {
                    outputs = false;
                }
            }
            else
            {
                servoModbus.Close();
                outputs = true;
            }
            return outputs;
        }

        public override async Task<bool> ExecuteAsync()
        {
            
            var result = await Task.Run(() => Execute());
            return result;
        }

        public override void Initialize()
        {
            isExecuted = false;
        }

        public override string Description => "Modbus通讯连接模块：用于配置和建立Modbus串口通讯，支持参数化端口、波特率、数据格式等。";
    }
}
